package com.glsc.ngateway.platform.service.other;

import com.glsc.ngateway.common.base.domain.mysql.gateway.other.Message;
import com.glsc.ngateway.common.base.repo.mysql.gateway.other.MessageRepository;
import com.glsc.ngateway.platform.service.MailService;
import com.glsc.ngateway.common.api.common.enums.DictConstant;
import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.platform.utils.SpecificationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Optional;

/**
 * 消息service
 */
@Service
public class MessageService {
    private static final Logger logger = LoggerFactory.getLogger(MessageService.class);

//    @Resource
//    private GatewayFeignService gatewayFeignService;

    @Resource
    private MailService mailService;

    @Resource
    private MessageRepository messageRepository;

    /**
     * 创建实体
     *
     * @param message
     */
    public Message saveMessage(Message message) {
        message.setOpAction(DictConstant.OP_ACTION_ADD);
        message.setOpSource(DictConstant.OP_SOURCE_WEB);
        return messageRepository.saveAndFlush(message);
    }

    /**
     * 修改实体
     *
     * @param message
     */
    public Message updateMessage(Message message) {
//todo,必填检查，如果抛出异常，需要抛出Error，而不是运行时异常，强制调用方处理异常
//        if(message.getId()==null){
//            throw GatewayException.error("更新消息时，消息Id为空");
//        }
        message.setOpAction(DictConstant.OP_ACTION_UPDATE);
        message.setOpSource(DictConstant.OP_SOURCE_WEB);
        return messageRepository.save(message);
    }

    /**
     * 分页条件查询
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<Message> findPage(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        return messageRepository.findAll(SpecificationUtil.buildSpecification(param, Message.class), pageable);
    }

    /**
     * 根据ID查询
     *
     * @return
     */
    public Message findById(Integer id) {
        Optional<Message> optional = messageRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        } else {
            return null;
        }
    }

//    /**
//     * 重新发送消息
//     *
//     * @param msgId
//     */
//    public void reSend(Integer msgId) {
//        Message mesaasge = this.findById(msgId);
//        if(mesaasge == null) throw GatewayException.error("消息实体不存在，不能重新发送消息！");
//
//        String msgType = mesaasge.getMsgType();
//        String msgTitle = mesaasge.getMsgTitle();
//        String msgContent = mesaasge.getMsgContent();
//        String msgReceiver = mesaasge.getMsgReceiver();
//        String filePath = mesaasge.getFilePath();
//        try {
//            if (DictConstant.MSG_TYPE_SMS.equals(msgType)) {
//                //发送短信
//                gatewayFeignService.sendShortMessage(msgReceiver, msgContent);
//            } else {
//                //解析邮件附件
//                List<File> attachmentList = new ArrayList<>();
//                if (StrUtil.isNotEmpty(filePath)) {
//                    String[] ss = filePath.split(",");
//                    for (String curr : ss) {
//                        attachmentList.add(new File(curr));
//                    }
//                }
//                //解析收件人
//                String[] msgReceivers = null;
//                if(StrUtil.isNotEmpty(msgReceiver)){
//                    msgReceivers = msgReceiver.split(",");
//                }
//                //发送邮件
//                mailService.sendMail(msgReceivers, null, msgTitle, msgContent, attachmentList);
//            }
//            mesaasge.setMsgStatus(DictConstant.MSG_STATUS_SUCCESS);
//            mesaasge.setMsgSuccessNum(mesaasge.getMsgSuccessNum() + 1);
//        } catch(Exception e) {
//            logger.error("消息重新发送异常!");
//            mesaasge.setMsgStatus(DictConstant.MSG_STATUS_FAILED);
//        }
//        //更新消息列表信息
//        this.updateMessage(mesaasge);
//    }
}
